package com.morningstar.common.dao.entity;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

import java.lang.reflect.Field;

/**
 * 分页查询通用
 * @author yanglianshan
 * @param <T>
 */
@Slf4j
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonQuery<P, T> {

    private Page<P> page;

    private T query;

    public QueryWrapper<P> getQueryWrapper() throws IllegalAccessException {
        QueryWrapper<P> queryWrapper = new QueryWrapper<>();
        if (ObjectUtils.isEmpty(query)) {
            return queryWrapper;
        }
        Field[] fields = query.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            //TODO 解决查询问题  使用注解
            queryWrapper.eq(!ObjectUtils.isEmpty(field.get(query)), field.getName(), field.get(query));
        }
        log.info(queryWrapper.getSqlSegment());
        return queryWrapper;
    }
}
